package su.msk.jet.team01.chatik.server.commands;

import su.msk.jet.team01.chatik.server.data.Client;
import su.msk.jet.team01.chatik.server.exception.InvalidArgumentException;

public class SendMessageCommand extends Command {

	public SendMessageCommand() {
		super("msg", 1);
	}

	@Override
	public void execute(final Client client, final String[] args) {
		String message = args[0];
		
		if (client.getName().isEmpty()) {
			client.send("You should select nick to send messages. Use /name command.");
			return;
		}

		try {
			validateLength(message, MAX_MESSAGE_LENGTH);
			client.getRoom().send("%s: %s", client.getName(), message);
		} catch (InvalidArgumentException e) {
			client.send("Message is too long. Maximum message length is %d.",
					MAX_MESSAGE_LENGTH);
		}
	}
}
